package com.lvwenguo.service.serviceImple;

import com.lvwenguo.dao.StockDao;
import com.lvwenguo.model.Stock;
import com.lvwenguo.service.StockInter;
import com.lvwenguo.utils.JdbcUtil;
import org.omg.CORBA.OBJ_ADAPTER;

import java.sql.Connection;
import java.util.List;
/*
* 实现接口
* */
public class StockImple implements StockInter {
    private Connection connection;
    private JdbcUtil jdbcUtil;
    private StockDao stockDao;
    public StockImple(){
        connection=JdbcUtil.getJdbcUtil().getConnertion();
        stockDao=new StockDao(connection);
    }
    /*
    * 查询所有,用于库存列表和变更记录两个
    * */
    @Override
    public List<Stock> selectStock(int i) {
        if(i==0){
            String sql="select st.product_bar_code,ca.category_name,pr.product_name,st.stock_count,su.supplier_name,su.contacts_name,su.tel from biz_product pr,biz_stock st,biz_category ca,biz_supplier su where st.product_bar_code=pr.bar_code and pr.category_id=ca.id and pr.supplier_id=su.id and pr.deleted=0";
            try {
                return stockDao.selectList(sql,Stock.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else{
            String sql="select smr.product_bar_code,ca.category_name,pr.product_name,smr.modify_count,smr.modify_type,\n" +
                    "smr.create_time from biz_product pr,biz_stock_modify_record smr,biz_category ca,biz_supplier su where smr.product_bar_code=pr.bar_code and pr.category_id=ca.id and pr.supplier_id=su.id and pr.deleted=0";
            try {
                return stockDao.selectList(sql,Stock.class);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return null;
    }
    /*
    * 模糊查询
    * */
    @Override
    public List<Stock> selectMohu(int i,String name) {
        if(i==0){
            String sql="select st.product_bar_code,ca.category_name,pr.product_name,st.stock_count,su.supplier_name,su.contacts_name,su.tel from biz_product pr,biz_stock st,biz_category ca,biz_supplier su where st.product_bar_code=pr.bar_code and pr.category_id=ca.id and pr.supplier_id=su.id and pr.deleted=0 and pr.product_name like ?";
            Object[] objects={"%"+name+"%"};
            try {
                return stockDao.selectList(sql,objects,Stock.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            String sql="select smr.product_bar_code,ca.category_name,pr.product_name,smr.modify_count,smr.modify_type,\n" +
                    "smr.create_time from biz_product pr,biz_stock_modify_record smr,biz_category ca,biz_supplier su where smr.product_bar_code=pr.bar_code and pr.category_id=ca.id and pr.supplier_id=su.id and pr.deleted=0 and pr.product_name like ?";
            Object[] objects={"%"+name+"%"};
            try {
                return stockDao.selectList(sql,objects,Stock.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
